package com.aier.common.common;

import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

/**
 * @author wangjingjing
 * @description 网信通发送短信接口
 * @time 2014-2-15
 * @version 1.0
 */
public class MessageSend_Mobile {
    
    private static final String URL = "http://www.js10088.com:9090/APIWebs/WebService.asmx";  //请求url
    private static final String ACCOUNT = "czzxwl";        //账号
    private static final String PASSWORD = "009988";    //密码
    
    public static String sendMessage(String message,String phone) {
        try {
            String password = Md5(PASSWORD);
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setOperationName(new QName("", "SendSms"));
            call.addParameter("UserName", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("Password", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("Mobile", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("Message", XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);
            call.setTargetEndpointAddress(new URL(URL));
            String result = (String) call.invoke(new Object[] { ACCOUNT,password, phone, message });
            return result;
            
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }

    }
    
    /**
     * MD5加密
     * @param plainText
     * @return
     */
    private static String Md5(String plainText ) {

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte[] b = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if(i<0) {
                    i+= Constant.num_256;
                }
                if(i<Constant.num_16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
    
            //System.out.println("result: " + buf.toString().toUpperCase());//32位的加密
            return buf.toString().toUpperCase();
            //System.out.println("result: " + buf.toString().substring(8,24).toUpperCase());//16位的加密

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }

    }
}
